using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manage_Default2 : System.Web.UI.Page
{
    protected Account acc;
    protected Functions func;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load();
        }
    }
    private void load()
    {
        int id = Convert.ToInt32(Request.QueryString["id"].ToString());
        if (id > 0)
        {
            loadTypeUser();
            acc = new Account();
            acc = acc.GetUser(id);
            txtUserName.Text = acc.username;
            txtName.Text = acc.name;
            txtEmail.Text = acc.email;
            txtIDCard.Text = acc.identify_card;
            txtAddress.Text = acc.address;
            DateTime tmpDate = acc.birthday;
            tbDate.Text = tmpDate.Day.ToString();
            ddlMonth.SelectedValue = tmpDate.Month.ToString();
            tbYear.Text = tmpDate.Year.ToString();
            hdEmail.Value = acc.email;
            hdID.Value = id.ToString();
            ddlType.SelectedValue = acc.type_user.ToString();
        }
    }

    private void loadTypeUser()
    {
        acc = new Account();
        DataTable tmp = acc.getAllAccountType();
        if (tmp.Rows.Count > 0)
        {
            ddlType.Items.Clear();
            for (int i = 0; i < tmp.Rows.Count; i++)
            {
                ddlType.Items.Add(new ListItem(tmp.Rows[i]["type_name"].ToString(), tmp.Rows[i]["ID"].ToString()));
            }
        }
        ddlType.DataBind();
    }
    //validate email
    protected void txtEmailCustomValidate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        acc = new Account();
        acc.email = txtEmail.Text;
        if (acc.checkEmail() && txtEmail.Text != "" && hdEmail.Value != txtEmail.Text)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            acc = new Account();
            func = new Functions();
            acc.name = txtName.Text;
            acc.birthday = func.getDateTime(tbDate.Text, ddlMonth.SelectedValue, tbYear.Text);
            acc.identify_card = txtIDCard.Text;
            acc.address = txtAddress.Text;
            acc.email = txtEmail.Text;
            acc.type_user = Convert.ToInt32(ddlType.SelectedValue.ToString());
            acc.userid = Convert.ToInt32(hdID.Value);
            if (acc.UpdateAccount())
            {
                Response.Redirect("./AccountNguoiDung.aspx");
            }
        }
    }
}
